package com.letoken.platform.repo.mall.dao;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.app.req.mall.BillingOutListReq;
import com.letoken.platform.repo.app.req.mall.EvaluateListReq;
import com.letoken.platform.repo.app.res.mall.BillingStatisticsInListRes;
import com.letoken.platform.repo.app.res.mall.BillingStatisticsOutListRes;
import com.letoken.platform.repo.app.res.mall.BillingStatisticsRes;
import com.letoken.platform.repo.app.res.mall.EvaluateListRes;
import com.letoken.platform.repo.mall.po.AppMallAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.mall.req.MallAccoutDetailReq;


import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 店铺账户表 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-04-21
 */
public interface AppMallAccountMapper extends BaseMapper<AppMallAccount> {
   Integer updateMAllAccountByUserId(@Param("balanceCAP")BigDecimal balanceCAP,@Param("balance")BigDecimal balance,@Param("contributing")BigDecimal contributing,@Param("bond")BigDecimal bond,@Param("version")Integer version,@Param("userId")Integer userId);


  List<MallAccoutDetailReq.Detail> getMallAccoutDetail(Page pagination, @Param("type") String type, @Param("userId")Integer userId, @Param("date")String date,@Param("payType")Integer payType);

   BigDecimal getSumMoneyByUserId(@Param("type") String type, @Param("userId")Integer userId, @Param("date")String date,@Param("payType")Integer payType);




   @Select("select IFNULL(sum(amount),0) as total,IFNULL(count(1),0) as num from app_user_assets_history where user_id = #{userId} and type = #{type} AND DATE_FORMAT(create_time, '%Y-%m') = #{date} ")
   BillingStatisticsRes getSumMoneyMonth(@Param("type") Integer type, @Param("userId")Integer userId, @Param("date")String date);


   @Select("select DATE_FORMAT(create_time, '%Y-%m-%d') as date , IFNULL(sum(amount),0)  as amount from app_user_assets_history where user_id = #{userId} and  create_time >= #{startTime} and create_time < #{endTime} and type = #{type} group by DATE_FORMAT(create_time, '%Y-%m-%d')")
   List<BillingStatisticsInListRes> billingStatisticsInList(@Param("type") Integer type, @Param("userId")Integer userId, @Param("startTime")String startTime , @Param("endTime")String endTime );



   Page<BillingStatisticsOutListRes> billingStatisticsOutList(@Param("page") Page<BillingStatisticsOutListRes> page , @Param("req") BillingOutListReq req);


    @Select("select * from app_mall_account where balance>0.01")
    List<AppMallAccount> getBalabce();



    @Select("select * from app_mall_account where mall_id = #{mallId} limit 1")
    AppMallAccount selectByMallId(@Param("mallId") Integer mallId);


    @Select("select id from app_mall_account where user_id = #{userId}")
    Integer selectByUserId(@Param("userId") Integer userId);

    @Select("select user_id from app_mall_account where mall_id = #{shopId}")
    Integer selectUserIdByShopId(@Param("shopId") Integer shopId);


 @Select("select ecommerce_contributing from app_mall_account where mall_id = #{shopId}")
 BigDecimal selectEcommerceContributingByShopId(@Param("shopId") Integer shopId);

    @Select("select * from app_mall_account where id = #{id} for update")
    AppMallAccount selectForUpdate(@Param("id") Integer id);



    @Select("select 1 from app_mall_account where mall_id = #{mallId} and bond >= 0.01 limit 1")
    Integer haveBond(@Param("mallId") Integer mallId);


    @Select("select 1 from app_mall_account where mall_id = #{mallId} and  (balance >= 0.01 or freeze >= 0.01) limit 1")
    Integer cancellationConditions(@Param("mallId") Integer mallId);

}
